<!--
 * @Author: ckk
 * @Date: 2022-08-15 17:32:04
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2023-08-21 16:23:13
 * @Description: 静态内容组件
-->
<template>
  <div class="static-layout">
     <one-static v-if="datas.setStyle.commodityType == 0" :datas="datas.setStyle" @goDetail="goDetail"/>
     <two-static v-if="datas.setStyle.commodityType == 1" :datas="datas.setStyle" @goDetail="goDetail"/>
     <three-static v-if="datas.setStyle.commodityType == 2" :datas="datas.setStyle" @goDetail="goDetail"/>
     <five-static v-if="datas.setStyle.commodityType == 3" :datas="datas.setStyle" @goDetail="goDetail"/>
     <four-static v-if="datas.setStyle.commodityType == 4" :datas="datas.setStyle" @goDetail="goDetail"/>
     <six-static v-if="datas.setStyle.commodityType == 5" :datas="datas.setStyle" @goDetail="goDetail"/>
     <seven-static v-if="datas.setStyle.commodityType == 6" :datas="datas.setStyle" @goDetail="goDetail"/>
  </div>
</template>

<script>
import { getLink, getVipStyle } from '../../common/comfun';
import { setRecordCountStat } from '@/utils/common.js';
import { Toast } from 'yj-mob-ui';
import { Base64 } from 'js-base64';
export default {
  name: 'contentLayout',
  components: {
    OneStatic: () => import('./OneStatic'),
    TwoStatic: () => import('./TwoStatic'),
    ThreeStatic: () => import('./ThreeStatic'),
    FourStatic: () => import('./FourStatic'),
    FiveStatic: () => import('./FiveStatic'),
    SixStatic: () => import('./SixStatic'),
    SevenStatic: () => import('./SevenStatic'),
    [Toast.name]: Toast
  },
  props: {
    datas: Object
  },
  data () {
    return {
      resid: '',
      residArr: [],
      recordList: [],
      bookList: [],
      vips: []
    };
  },
  created () {
    this.datas.setStyle.imageList.forEach(item => {
      this.residArr.push(item.id);
    });
    this.resid = this.residArr.join(',');
    this.getVip();
  },
  mounted () {
    setRecordCountStat(this.resid).then(data => {
      this.recordList = JSON.parse(JSON.stringify(data));
      this.datas.setStyle.imageList.map((item) => {
        this.recordList.map((ite) => {
          if (item.id === ite.resId) {
            this.$set(item, 'viewCount', ite.viewCount);
          } else {
            const oldtimestamp = new Date('2023-02-22').getTime(),
              newtimestamp = Date.now(),
              timeSpan = Math.floor((newtimestamp - oldtimestamp) / (24.0 * 3600.0)), // 时间戳计算
              viewCount = 30 + (Math.floor(Math.random() * 20) + 1), // 生成随机数
              num = 299 * (viewCount + 1) + Math.floor(99 * Math.log(1 + 24 * timeSpan));// 计算公式
            this.$set(item, 'viewCount', num);
          }
        });
      });
    });
  },
  methods: {
    getVip () {
      if (localStorage.getItem('vipsArr') !== null) {
        const newVipsArr = JSON.parse(localStorage.getItem('vipsArr'));
        this.datas.setStyle.imageList.forEach(item => {
          newVipsArr && newVipsArr.forEach(ite => {
            if (item.id === ite.id) {
              this.vips = ite.vips;
            }
          });
          this.$set(item, 'vipObj', getVipStyle(this.vips, item.id));
        });
      }
    },
    goDetail (item) {
      if (item.url !== undefined) {
        if (item.url.includes('.json?type=7100854158334001076') || item.url.includes('.json?type=7543721344441725193')) {
          const requestParam = {
            title: item.text,
            templateUrl: Base64.encode(item.url)
          };
          this.$router.push({ path: '../../secondPage', query: requestParam });
        } else {
          window.location.href = getLink(item.url);
        }
      } else {
        this.$toast('暂无跳转链接');
      }
    }
  }
};
</script>
<style scoped lang="scss">
.static-layout{
  margin-bottom:10px;
}
</style>
